﻿using System;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Collections.Generic;
using System.Linq;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SL.Demo.RIAServices.Web;
using UI.Web.SL.Demo.SysAdmin.Controls;

namespace UI.Web.SL.Demo.SysAdmin
{
    public partial class MainFrame : UserControl
    {

        private SysAdminDContext _sysadminDContext = new SysAdminDContext();
        private LoadOperation<MenuInfo> _loadMenuListOperation;
        private IEnumerable<MenuInfo> _MenuList;
        private void InitMenu()
        {
            LoadMenuList(null);
        }

        public IEnumerable<MenuInfo> MenuList
        {
            get { return _MenuList; }
            set
            {
                _MenuList = value;
            }
        }

        public void LoadMenuList(object o)
        {
            _loadMenuListOperation = _sysadminDContext.Load(_sysadminDContext.GetMenusQuery());
            _loadMenuListOperation.Completed += new EventHandler(_loadMenuListOperation_Completed);
        }

        void _loadMenuListOperation_Completed(object sender, EventArgs e)
        {
            this.MenuList = _loadMenuListOperation.Entities;
            Common.ViewUtility.AllMenu = MenuList.ToList();
            InitTopMenu();
        }

        void InitTopMenu()
        {
            var tops = Common.ViewUtility.AllMenu
                .Where(m => m.ParentId == Guid.Empty)
                .OrderByDescending(m => m.OrderNum);
            foreach (var m in tops)
            {
                var topM = new HeadBtn();
                topM.DataContext = m;
                topM.MouseLeftButtonUp += new MouseButtonEventHandler(topM_MouseLeftButtonUp);
                TopMenuS.Children.Insert(2,topM);
            }
        }

        void topM_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var tBTN = sender as HeadBtn;
            var tMenu = tBTN.DataContext as MenuInfo;
            lblMenuText.Content = tMenu.MenuName;
            var subs = Common.ViewUtility.AllMenu
                .Where(m => m.ParentId == tMenu.Id)
                .OrderBy(m => m.OrderNum);
            left_panel.ItemsSource = subs;
        }
    }
}
